/**
 * $Id: Main.java 23 2006-09-17 20:02:05Z willi.w $
 */
package at.wwu.tunes2db;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFileChooser;

import org.apache.log4j.*;
import org.xml.sax.*;

import at.wwu.tunes2db.export.*;
import at.wwu.tunes2db.xml.*;

/**
 * @author willi.w
 * call the parser 
 */
public class App {

    private static final Logger log = Logger.getLogger(App.class.getName());

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Configuration conf = null;
            initLogging();
            Commandline cmd = Commandline.parse(args);
            if (cmd.getInput() == null) {
                selectInput(cmd);
            }

            conf = parseConf();
            parseLib(cmd, conf);

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private static void selectInput(Commandline cmd) {
        // gui select input
        JFileChooser fc = new JFileChooser(".");
        ExampleFileFilter filter = new ExampleFileFilter();
        filter.addExtension("xml");
        filter.setDescription("iTunes XML Library");
        fc.setFileFilter(filter);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            log.info("You chose to open this file: " + fc.getSelectedFile().getName());
            cmd.setInput(fc.getSelectedFile().getAbsolutePath());
        } else {
            System.exit(0);
        }
    }

    private static void initLogging() {
        Properties logProps = new Properties();
        logProps.setProperty("log4j.rootLogger", "DEBUG,A1");
        logProps.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender");
        logProps.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
        logProps.setProperty("log4j.appender.A1.layout.ConversionPattern", "%-4r [%t] %-5p %c %x - %m%n");
        logProps.setProperty("log4j.threshold", "DEBUG");
        PropertyConfigurator.configure(logProps);
        log.debug("Logging initialized");
    }

    private static void parseLib(Commandline cmd, Configuration conf) throws SAXException, FileNotFoundException,
            IOException {
        //Destination db = new Derby();
        //Destination stat = new ParsingStats();
        //Destination file = new ExportFile();
        Destination profiler = Profiler.getInstance();
        LibraryContentHandler libContentHandler = new LibraryContentHandler();
        ErrorHandler libErrorHandler = new LibraryErrorHandler();
        //libHandler.addDestination(db);
        //handler.addDestination(stat); // no stats for now
        
        //libHandler.addDestination(file);
        libContentHandler.addDestination(profiler);
        Parser libParser = new Parser(cmd, libContentHandler, libErrorHandler);
        libParser.parse();
    }

    private static Configuration parseConf() throws SAXException, FileNotFoundException, IOException {
        // Parse the configuration
        ConfigurationHandler confHandler = new ConfigurationHandler();
        Configuration conf = new Configuration(confHandler);
        conf.parse();
        return conf;
    }

}
